package org.pictual.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import wikiapi.MediaWikiAPI;
import wikiapi.WikiAPI;


public class WikiObtieneDatos {

	protected static Logger log= Logger.getLogger(WikiObtieneDatos.class);

	public static String getData(String query, String idioma_wiki){
		StringBuffer cadena_wiki = new StringBuffer();

		//		File f = new File
		//		BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(), "utf-8"));

		//		http://es.wiktionary.org/w/api.php?format=json&action=query&titles=casa&rvprop=content&prop=revisions&redirects=1

		// Busco con la API de Wiktionary en la siguiente ubicación: www.(idioma_destino).wiktionary.org/wiki/(palabra)
//		WikiAPI wiki = new MediaWikiAPI();
		try {
//			String wiki_url="http://"+idioma_wiki+".wiktionary.org/wiki/";
//			log.info("Buscando en: "+wiki_url);
//			
//			if (!wiki.connect(wiki_url)) {
//				log.debug("Unable to connect to the wiki.");

				String url = "http://"+idioma_wiki+".wiktionary.org/w/api.php?format=txt&action=query&titles="+query+"&rvprop=content&prop=revisions&redirects=1";
				
//				System.out.println(readTXTFromUrl(url));
//				JSONObject json = readJsonFromUrl(url);

				cadena_wiki.append(readTXTFromUrl(url));
//			}else{
////				if(idioma_wiki.equals("es"))cadena_wiki.append(new String(wiki.getPage(query).getBytes(), "UTF-8"));
////				if(idioma_wiki.equals("fr"))cadena_wiki.append(new String(wiki.getPage(query).getBytes(), "UTF-16"));
//			
//				cadena_wiki.append(new String(wiki.getPage(query).getBytes(), "UTF-8"));
//			}

			log.info("Obtenido el texto wiki "+ idioma_wiki+" de: "+query);

		} catch (Exception e) {
			log.error("Problem during connect: " + e);
		}

		// lo pasamos a minusculas
		//		cadena_wiki=cadena_wiki.toLowerCase();
		//		log.info("Cadena de wiki pasada a minusculas");

		return cadena_wiki.toString();
	}

	private static String readAll(Reader rd) throws IOException {
		StringBuilder sb = new StringBuilder();
		int cp;
		while ((cp = rd.read()) != -1) {
			sb.append((char) cp);
		}
		return sb.toString();
	}

	public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
		InputStream is = new URL(url).openStream();
		try {
			BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
			String jsonText = readAll(rd);
			JSONObject json = new JSONObject(jsonText);
			return json;
		} finally {
			is.close();
		}
	}
	
	public static String readTXTFromUrl(String url) throws IOException {
		InputStream is = new URL(url).openStream();
		try {
			BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
			String jsonText = readAll(rd);
			return jsonText;
		} finally {
			is.close();
		}
	}


	public static void main(String[] args) {
		String palabra = "coche";
		String idioma_wiki = "es";
		String idioma_palabra = "es";
		String cadena_wiki = WikiObtieneDatos.getData(palabra, idioma_wiki);

		System.out.println(cadena_wiki);

		WikiTrataDatos wtd = new WikiTrataDatos(palabra, cadena_wiki, idioma_wiki, idioma_palabra);
	}


}
